package cn.xiaoming.car.server.content.controller;

import cn.xiaoming.car.pojo.car.pojo.entity.Series;
import cn.xiaoming.car.pojo.car.pojo.param.BrandAddNewParam;
import cn.xiaoming.car.pojo.car.pojo.param.BrandUpdateInfoParam;
import cn.xiaoming.car.pojo.car.pojo.vo.BrandListItemVO;
import cn.xiaoming.car.pojo.car.pojo.vo.BrandStandardVO;
import cn.xiaoming.car.server.ISeriesService;
import cn.xiaoming.car.server.jsonresult.JsonResult;
import cn.xiaoming.car.server.pojo.vo.PageData;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * 处理车系相关请求的控制器
 *
 * @author java@tedu.cn
 * @version 0.0.1
 */
@Slf4j
@RestController
@RequestMapping("/series")
@Validated
@Api(tags = "02. 车系管理模块")
public class SeriesController {

    @Autowired
    private ISeriesService seriesService;

    public SeriesController() {
        log.info("创建控制器对象：BrandController");
    }
/*
    // http://localhost:9180/brand/add-new
    @PostMapping("/add-new")
    @ApiOperation("添加车系")
    @ApiOperationSupport(order = 100)
    public JsonResult addNew(@Valid @RequestBody BrandAddNewParam brandAddNewParam) {
        log.debug("开始处理【添加车系】的请求，参数：{}", brandAddNewParam);
        seriesService.addNew(brandAddNewParam);
        return JsonResult.ok();
    }

    // http://localhost:9180/brand/delete
    @PostMapping("/{id:[0-9]+}/delete")
    @ApiOperation("根据ID删除车系")
    @ApiOperationSupport(order = 200)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "车系ID", required = true, dataType = "long")
    })
    public JsonResult delete(@Range(min = 1, message = "请提交有效的车系ID值！") @PathVariable Long id) {
        log.debug("开始处理【根据ID删除车系】的请求，参数：{}", id);
        seriesService.delete(id);
        return JsonResult.ok();
    }

    // http://localhost:9180/brand/update
    @PostMapping("/{id:[0-9]+}/update")
    @ApiOperation("修改车系详情")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "车系ID", required = true, dataType = "long")
    })
    public JsonResult updateInfoById(@PathVariable @Range(min = 1, message = "请提交有效的车系ID值！") Long id,
                                     @Valid @RequestBody BrandUpdateInfoParam brandUpdateInfoParam) {
        log.debug("开始处理【修改车系详情】的业务，参数：{}", brandUpdateInfoParam);
        seriesService.updateInfoById(id, brandUpdateInfoParam);
        return JsonResult.ok();
    }

    //http://localhost:8888/brands/id/state
    @PostMapping("{id:[0-9]+}/{state}")
    @ApiOperation("启/禁用车系")
    @ApiOperationSupport(order = 300)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "车系ID", required = true, dataType = "long"),
            @ApiImplicitParam(name = "state", value = "车系状态码", required = true, dataType = "string" ,example = "enable/disable")
    })
    public JsonResult updateState(@PathVariable @Range(min = 1, message = "请提交有效的车系ID值！") Long id,@PathVariable String state) {
        log.debug("开始处理【启/禁用车系】的请求id：{}，state：{}", id, state);
        seriesService.updateState(id,state);
        return JsonResult.ok();
    }

    // http://localhost:9180/brand/standard
    @GetMapping("/standard")
    @ApiOperation("根据ID查询车系详情")
    @ApiOperationSupport(order = 410)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "车系ID", required = true, dataType = "long")
    })
    public JsonResult getStandardById(@Range(min = 1, message = "根据ID查询车系详情失败，请提交合法的ID值！")
                                      @RequestParam Long id) {
        log.debug("开始处理【根据ID查询车系详情】的请求，参数：{}", id);
        BrandStandardVO result = seriesService.getStandardById(id);
        return JsonResult.ok(result);
    }

*/

    // http://localhost:8888/brands?page=1
    @GetMapping("/list/{page}")
    @ApiOperation("查询车系列表")
    @ApiOperationSupport(order = 420)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", paramType = "query")
    })
    public JsonResult list(@PathVariable @Range(min = 1, message = "查询车系列表失败，请提供正确的页码值！") Long page) {
        log.debug("开始处理【查询车系列表】的请求，页码：{}", page);
        if (page == null || page < 1) {
            page = 1L;
        }
        Page<Series> pageData = seriesService.list(page);
        return JsonResult.ok(pageData);
    }

}